var isDivHiden = {starting: false, ending: true, date: true, startingEnding: true, advanced: true};

$(document).ready(function () {
    $('#endingPlaceDiv').hide();
    $('#departureDateDiv').hide();
    $('#startingPlaceEndingPlaceDiv').hide();
    $('#advancedSearchDiv').hide();
    loadCountrys();
    initCountryDropDowns();
    $('#startingPlaceDepartueDateFieldSearch').datetimepicker({
        step: 5,
        startDate: '+1970/01/02',
        defaultDate: '+1970/01/02',
        minDate: '+1970/01/02',
        defaultTime: '09:00'
    });
    $('#startingPlaceDepartueDateFieldSearch2').datetimepicker({
        step: 5,
        startDate: '+1970/01/02',
        defaultDate: '+1970/01/02',
        minDate: '+1970/01/02',
        defaultTime: '09:00'
    });
    $('#departueDateFieldSearch').datetimepicker({
        step: 5,
        startDate: '+1970/01/02',
        defaultDate: '+1970/01/02',
        minDate: '+1970/01/02',
        defaultTime: '09:00'
    });
    $("#searchOptionsDropDown").change(function () {
        var selected = $("#searchOptionsDropDown").val();
        if (selected === "0") {
            onSeeStartingSearch();
        } else if (selected === "1") {
            onSeeEndingSearch();
        } else if (selected === "2") {
            onSeeDateSearch();
        } else if (selected === "3") {
            onSeeStartingEndingSearch();
        } else if (selected === "4") {
            onSeeAdvancedSearch();
        }
    });
});

function onDoHide() {
    if (isDivHiden.starting) {
        $('#startingPlaceDiv').hide(2000);
    } else {
        $('#startingPlaceDiv').show(1000);
    }
    if (isDivHiden.ending) {
        $('#endingPlaceDiv').hide(2000);
    } else {
        $('#endingPlaceDiv').show(1000);
    }
    if (isDivHiden.date) {
        $('#departureDateDiv').hide(2000);
    } else {
        $('#departureDateDiv').show(1000);
    }
    if (isDivHiden.startingEnding) {
        $('#startingPlaceEndingPlaceDiv').hide(2000);
    } else {
        $('#startingPlaceEndingPlaceDiv').show(1000);
    }
    if (isDivHiden.advanced) {
        $('#advancedSearchDiv').hide(2000);
    } else {
        $('#advancedSearchDiv').show(1000);
    }
}

function onSeeStartingSearch() {
    isDivHiden.starting = false;
    isDivHiden.ending = true;
    isDivHiden.date = true;
    isDivHiden.startingEnding = true;
    isDivHiden.advanced = true;
    onDoHide();
}
function onSeeEndingSearch() {
    isDivHiden.starting = true;
    isDivHiden.ending = false;
    isDivHiden.date = true;
    isDivHiden.startingEnding = true;
    isDivHiden.advanced = true;
    onDoHide();
}
function onSeeDateSearch() {
    isDivHiden.starting = true;
    isDivHiden.ending = true;
    isDivHiden.date = false;
    isDivHiden.startingEnding = true;
    isDivHiden.advanced = true;
    onDoHide();
}
function onSeeStartingEndingSearch() {
    isDivHiden.starting = true;
    isDivHiden.ending = true;
    isDivHiden.date = true;
    isDivHiden.startingEnding = false;
    isDivHiden.advanced = true;
    onDoHide();
}
function onSeeAdvancedSearch() {
    isDivHiden.starting = true;
    isDivHiden.ending = true;
    isDivHiden.date = true;
    isDivHiden.startingEnding = true;
    isDivHiden.advanced = false;
    onDoHide();
}

function loadCountrys() {
    getAllCountrys(function (arrayCountry) {
        for (var i in arrayCountry) {
            $("#startingPlaceCountryDropDownSearch").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
            $("#endingPlaceCountryDropDownSearch").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
            $("#startingPlaceCountryDropDownSearch2").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
            $("#endingPlaceCountryDropDownSearch2").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
        }
    });
}

function initCountryDropDowns() {
    $("#startingPlaceCountryDropDownSearch").change(function () {
        refreshDropDown("#startingPlaceCountryDropDownSearch", "#startingPlaceAddressDropDownSearch");
    });
    $("#endingPlaceCountryDropDownSearch").change(function () {
        refreshDropDown("#endingPlaceCountryDropDownSearch", "#endingPlaceAddressDropDownSearch");
    });
    $("#startingPlaceCountryDropDownSearch2").change(function () {
        refreshDropDown("#startingPlaceCountryDropDownSearch2", "#startingPlaceAddressDropDownSearch2");
    });
    $("#endingPlaceCountryDropDownSearch2").change(function () {
        refreshDropDown("#endingPlaceCountryDropDownSearch2", "#endingPlaceAddressDropDownSearch2");
    });
}

function refreshDropDown(country, address) {
    if ($(country).val() !== "-1") {
        getAllAdressFromCountry($(country).val(), function (arrayAdress) {
            $(address)
                    .find('option')
                    .remove()
                    .end()
                    .append('<option value="-1">Select a address</option>');

            for (var i in arrayAdress) {
                $(address).append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
            }
        });
    }
}

var searchTable = null;
function onSearchTripClick() {
    if (!isDivHiden.startingEnding) {
        initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/start=" + $("#startingPlaceAddressDropDownSearch2").val() + "&end=" + $("#endingPlaceAddressDropDownSearch2").val() + "&date=a/");
    }
}

function onSearchTripStartingClick() {
    $("#startingPlaceAddressDropDownSearch").parent().removeClass('has-error has-feedback').find('span').remove();
    var address = $("#startingPlaceAddressDropDownSearch").val();
    if (address === "-1") {
        $("#startingPlaceAddressDropDownSearch").parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span>Select a starting address!</span>').insertAfter("#startingPlaceAddressDropDownSearch");
        return;
    }
    var dateField = $('#startingPlaceDepartueDateFieldSearch').val();
    if (!dateField) {
        initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/start=" + address + "&date=-1/");
    } else {
        initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/start=" + address + "&date=" + Date.parse(dateField) + "/");
    }
}

function onSearchTripEndingClick() {
    $("#endingPlaceAddressDropDownSearch").parent().removeClass('has-error has-feedback').find('span').remove();
    var address = $("#endingPlaceAddressDropDownSearch").val();
    if (address === "-1") {
        $("#endingPlaceAddressDropDownSearch").parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span>Select a ending address!</span>').insertAfter("#endingPlaceAddressDropDownSearch");
        return;
    }
    initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/end=" + address + "/");
}

function onSearchTripDepartureClick() {
    $('#departueDateFieldSearch').parent().removeClass('has-error has-feedback').find('span').remove();
    var dateField = $('#departueDateFieldSearch').val();
    if (!dateField) {
        $('#departueDateFieldSearch').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#departueDateFieldSearch');
        return;
    }
    var date = new Date(Date.parse(dateField));
    if (!date) {
        $('#dateField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Insert a date!</span>').insertAfter('#departueDateFieldSearch');
        return;
    }
    initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/date=" + Date.parse(dateField) + "/");
}

function onSearchTripStartingEndingClick() {
    $("#startingPlaceAddressDropDownSearch2").parent().removeClass('has-error has-feedback').find('span').remove();
    var start = $("#startingPlaceAddressDropDownSearch2").val();
    if (start === "-1") {
        $("#startingPlaceAddressDropDownSearch2").parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span>Select a starting address!</span>').insertAfter("#startingPlaceAddressDropDownSearch2");
        return;
    }

    $("#endingPlaceAddressDropDownSearch2").parent().removeClass('has-error has-feedback').find('span').remove();
    var end = $("#endingPlaceAddressDropDownSearch2").val();
    if (end === "-1") {
        $("#endingPlaceAddressDropDownSearch2").parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span>Select a starting address!</span>').insertAfter("#endingPlaceAddressDropDownSearch2");
        return;
    }

    var dateField = $('#startingPlaceDepartueDateFieldSearch2').val();
    if (!dateField) {
        initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/start=" + start + "&end=" + end + "&date=-1/");
    } else {
        initResultTable("/Trabalho_PD/webresources/WebServiceBoleias/searchTrips/start=" + start + "&end=" + end + "&date=" + Date.parse(dateField) + "/");
    }
}

function initResultTable(searchUrl) {
    if (searchTable === null) {
        searchTable = $('#table_trips').DataTable({
            ajax: {
                url: searchUrl,
                type: "GET",
                dataSrc: "trip"
            },
            order: [[0, "asc"]],
            columns: [
                {data: 'departueDate'},
                {data: 'arrivalDate'},
                {data: 'departue_address'},
                {data: 'destination_address'},
                {data: 'car_brand'},
                {
                    "className": 'details-control',
                    "orderable": false,
                    "data": null,
                    "defaultContent": '<i class="btn btn-info fa fa-search fa-2x"></i>'
                }
            ]
        });
    } else {
        searchTable.ajax.url(searchUrl).load();
    }
}